package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;

import Entities.ExecutionEntity;

import common.ChatIF;
/**
 * 
 * @author Guy Franco
 * This GUi class presents an execution form
 *
 */
public class ViewExecutedPanel extends JPanel implements ChatIF {
	
	private JTextField viewingTextField;
	private JTextField firstNameField;
	private JTextField lastNameField;
	private JTextField emailField;
	private JTextField departmentField;
	private JTextField titleField; 
	private JTextField phoneField;
	private JTextArea exDetailsTextArea;
	private JTextArea wasExtendedTextArea;
	private String exeID;
	private String aid;
	private JButton btnClose;
	private AcademicFrame frame;
	private JPanel previousPanel;
	
	public ViewExecutedPanel(String exeID, AcademicFrame frame, JPanel previousPanel)
	{
		this.exeID = exeID;
		this.frame = frame;
		this.previousPanel=previousPanel;

		initialize();
		
		
		
	}
		private void initialize()
		{
			setLayout(null);
				
			JLabel lblViewing = new JLabel("Viewing Executed");
			lblViewing.setBounds(116, 33, 129, 14);
			add(lblViewing);
			
			viewingTextField = new JTextField();
			viewingTextField.setEditable(false);
			viewingTextField.setBounds(253, 30, 99, 20);
			add(viewingTextField);
			viewingTextField.setColumns(10);
			
			JLabel lblFirstName = new JLabel("First name");
			lblFirstName.setBounds(32, 98, 73, 14);
			add(lblFirstName);
			
			firstNameField = new JTextField();
			firstNameField.setEditable(false);
			firstNameField.setBounds(104, 95, 141, 20);
			add(firstNameField);
			firstNameField.setColumns(10);
			
			JLabel lblInitiatorDetails = new JLabel("Execution Leader Details:");
			lblInitiatorDetails.setBounds(32, 68, 154, 14);
			add(lblInitiatorDetails);
			
			JLabel lblLastName = new JLabel("Last name");
			lblLastName.setBounds(32, 131, 73, 14);
			add(lblLastName);
			
			lastNameField = new JTextField();
			lastNameField.setEditable(false);
			lastNameField.setColumns(10);
			lastNameField.setBounds(104, 128, 141, 20);
			add(lastNameField);
			
			emailField = new JTextField();
			emailField.setEditable(false);
			emailField.setColumns(10);
			emailField.setBounds(351, 95, 141, 20);
			add(emailField);
			
			JLabel lblEmail = new JLabel("E-mail");
			lblEmail.setBounds(279, 98, 73, 14);
			add(lblEmail);
			
			JLabel lblDepartment = new JLabel("Department");
			lblDepartment.setBounds(279, 131, 73, 14);
			add(lblDepartment);
			
			departmentField = new JTextField();
			departmentField.setEditable(false);
			departmentField.setColumns(10);
			departmentField.setBounds(351, 128, 141, 20);
			add(departmentField);
			
			titleField = new JTextField();
			titleField.setEditable(false);
			titleField.setColumns(10);
			titleField.setBounds(104, 162, 141, 20);
			add(titleField);
			
			JLabel lblTitle = new JLabel("Title");
			lblTitle.setBounds(32, 165, 73, 14);
			add(lblTitle);
			
			phoneField = new JTextField();
			phoneField.setEditable(false);
			phoneField.setColumns(10);
			phoneField.setBounds(351, 162, 141, 20);
			add(phoneField);
			
			JLabel lblPhone = new JLabel("Phone");
			lblPhone.setBounds(279, 165, 73, 14);
			add(lblPhone);
			
			JLabel lblRequieredChangeDescription = new JLabel("Execution Details");
			lblRequieredChangeDescription.setBounds(32, 256, 205, 14);
			add(lblRequieredChangeDescription);
			
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(32, 281, 461, 100);
			add(scrollPane);
			
			exDetailsTextArea = new JTextArea();
			scrollPane.setViewportView(exDetailsTextArea);
			exDetailsTextArea.setBackground(UIManager.getColor("TextArea.disabledBackground"));
			exDetailsTextArea.setEditable(false);
			
			JLabel lblExpectedResultsAndBenefits = new JLabel("Was Extended?");
			lblExpectedResultsAndBenefits.setBounds(32, 392, 205, 14);
			add(lblExpectedResultsAndBenefits);
			
			JScrollPane scrollPane_1 = new JScrollPane();
			scrollPane_1.setBounds(32, 417, 457, 105);
			add(scrollPane_1);
			
			wasExtendedTextArea = new JTextArea();
			wasExtendedTextArea.setEditable(false);
			scrollPane_1.setViewportView(wasExtendedTextArea);
			wasExtendedTextArea.setBackground(UIManager.getColor("TextArea.disabledBackground"));
			
			btnClose = new JButton("Close");
			btnClose.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					frame.setSize(640, 540);
					frame.setContentPane(previousPanel);	
				}
			});
			btnClose.setBounds(229, 558, 91, 23);
			add(btnClose);
			
			JButton btnFetch = new JButton("Fetch");
			btnFetch.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					ArrayList<Object> exe =new ArrayList<Object>();
					exe.add("getExecute");
					exe.add(exeID);
					AcademicFrame.myclient.sendMessageToServer(exe);
				}
			});
			btnFetch.setBounds(373, 29, 89, 23);
			add(btnFetch);
		}

	@Override
	public void display(Object message) {
		// TODO Auto-generated method stub

		ArrayList<Object> answer = new ArrayList<Object>();
		answer = (ArrayList<Object>)message;

		
		
		if (	((String)(answer.get(0))).equalsIgnoreCase("getExecute")	)
		{
			if (	((String)(answer.get(1))).equalsIgnoreCase("Valid")	)
			{
				ExecutionEntity exeE = (ExecutionEntity)(answer.get(2));
				
				// Start filling the form
				this.viewingTextField.setText(exeID);				
				this.exDetailsTextArea.setText(exeE.getExecutionDetails());
				this.wasExtendedTextArea.setText(exeE.getWasExtended());
				ArrayList<Object> ridToUid = new ArrayList<Object>();
				ridToUid.add("ridToUid");
				ridToUid.add(exeID);
				AcademicFrame.myclient.sendMessageToServer(ridToUid);
				
			}
			else
			{
				(new Dialog ( (String)(answer.get(1)) )).setVisible(true);
				frame.setContentPane(previousPanel);
				frame.setSize(500, 400);
			}

		}
		
		else
			if (	((String)(answer.get(0))).equalsIgnoreCase("userInfo")	)
				{
					this.firstNameField.setText((String)answer.get(1));
					this.lastNameField.setText((String)answer.get(2));
					this.emailField.setText((String)answer.get(3));
					this.titleField.setText((String)answer.get(4));
					this.departmentField.setText((String)answer.get(5));
					this.phoneField.setText((String)answer.get(6));
					
				}
			else
				if(((String)(answer.get(0))).equalsIgnoreCase("ridToAid"))
				{
					if(((String)(answer.get(1))).equalsIgnoreCase("gotit"))
					{
						ArrayList<Object> getInitiatorInfo = new ArrayList<Object>();
						getInitiatorInfo.add("getInfo");
						getInitiatorInfo.add((String)(answer.get(2)));
						
						AcademicFrame.myclient.sendMessageToServer(getInitiatorInfo);
					}
					else
						if(((String)(answer.get(1))).equalsIgnoreCase("fail"))
						{
							(new Dialog ( "Fail")).setVisible(true);
							frame.setContentPane(previousPanel);
							frame.setSize(500, 400);
						}
				}
				
				
		
	}
}


